package com.yy.project.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.project.entity.Task;
import com.yy.project.mapper.TaskMapper;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class TaskService {
    
    @Inject
    private TaskMapper taskMapper;
    
    public List<Task> getAllTasks() {
        return taskMapper.selectList(null);
    }
    
    public Task getTaskById(Long id) {
        return taskMapper.selectById(id);
    }
    
    public Task createTask(Task task) {
        task.setCreateTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        taskMapper.insert(task);
        return task;
    }
    
    public Task updateTask(Task task) {
        task.setUpdateTime(LocalDateTime.now());
        taskMapper.updateById(task);
        return task;
    }
    
    public boolean deleteTask(Long id) {
        return taskMapper.deleteById(id) > 0;
    }
    
    public Page<Task> getTasksByPage(int current, int size) {
        Page<Task> page = new Page<>(current, size);
        return taskMapper.selectPage(page, null);
    }
}